/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test.SpringMVC;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import aorey.config.FrameworkConfig;
import aorey.database.DataBaseFactory;
import aorey.nosql.EhcacheUtil;
import aorey.web.WebTemplate;

/**
 *
 * @author csq
 */

@Controller
@RequestMapping("/mvc")
public class mvcController {
    @SuppressWarnings("unchecked")
	@RequestMapping("/hello")
    public String hello(HttpServletRequest request){ 
    	String str = (String) request.getServletContext().getAttribute("aaaa");
    	System.out.println(str);
    	DataBaseFactory db = new DataBaseFactory();
		EhcacheUtil framework = (EhcacheUtil) request.getServletContext().getAttribute("aorey");
		HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
		try {
			db.getConnection(dataBaseDrivers.get("pgsql").getConnection());
			String rs = db.getResult("SELECT id, f1, f2 FROM public.test");
    		System.out.println(rs);
			
		} catch (Exception e) {
			
			
		}
		finally {
			db.close();
		}
        return "hello";
    }
    
    
    @RequestMapping("/abc")
    public String abc(HttpServletRequest request,HttpServletResponse response)
    {
    	try {
    		WebTemplate template = new WebTemplate(request.getRealPath("")+FrameworkConfig.templateConfig);
    		
    		
    		HashMap<String, Object> data = new HashMap<>();
    		data.put("key1", "aaaaaaaaaaaaaaaaaa");
    		data.put("key2","cccccccccccccccccccc");
    		template.bind(data);
    		String html = template.getTemplate("/test_tpl.html");
    		response.getWriter().println(html);
    		response.getWriter().close();
		} catch (Exception e) {
			
		}
    	finally {
    		
		}
    	
    	
    	return "";
    }
}
